/* NOTICE
    OpenJaWS - Open Java Weather Station
    
    Copyright (C) 2008 Grant Gardner <grant@lastweekend.com.au>
    
    OpenJaWS is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
NOTICE *//**
 * 
 */

package au.com.lastweekend.openjaws.units;

public enum TemperatureUnit implements Unit {
    CELSIUS('C') {

        public double convert(double temp, TemperatureUnit unitFrom) {

            switch (unitFrom) {
                case FAHRENHEIT :
                    return ((temp - 32.0) / 1.8);
                case KELVIN :
                    return temp + ABSOLUTE_ZERO;
                default :
                    return temp;
            }
        }

    },

    FAHRENHEIT('F') {

        public double convert(double temp, TemperatureUnit unitFrom) {

            switch (unitFrom) {
                case CELSIUS :
                    return (temp * 1.8) + 32;
                case FAHRENHEIT :
                    return temp;
                default :
                    return convert(CELSIUS.convertFrom(temp, unitFrom), CELSIUS);
            }
        }

    },

    KELVIN('K') {

        public double convert(double temp, TemperatureUnit unitFrom) {

            switch (unitFrom) {
                case CELSIUS :
                    return temp - ABSOLUTE_ZERO;
                case KELVIN :
                    return temp;
                default :
                    return convert(CELSIUS.convertFrom(temp, unitFrom), CELSIUS);
            }
        }

    };

    private static final double ABSOLUTE_ZERO = -273.15;
    private String utfSymbol;

    private TemperatureUnit(char symbol) {

        this.utfSymbol = "°" + symbol;
    }

    public double convertFrom(double value, Unit unit) {

        TemperatureUnit tempUnit = (TemperatureUnit) unit;
        return convert(value, tempUnit);
    }

    public abstract double convert(double temp, TemperatureUnit unitFrom);

    public String toString() {

        return utfSymbol;
    }

    public static void main(String[] args) {

        TemperatureUnit[] units = TemperatureUnit.values();
        for (TemperatureUnit prefix : units) {
            System.out.println(prefix.name() + ":" + prefix.toString());
        }

    }
}